{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2]\n",
      "[ 1.          2.71828183  7.3890561 ]\n",
      "[ 0.          1.          1.41421356]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "B = np.arange(3)\n",
    "print(B)\n",
    "print(np.exp(B))#e的幂\n",
    "print(np.sqrt(B))#开方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 6.  6.  1.  0.]\n",
      " [ 2.  0.  0.  2.]\n",
      " [ 9.  7.  2.  7.]]\n",
      "--------\n",
      "[ 6.  6.  1.  0.  2.  0.  0.  2.  9.  7.  2.  7.]\n",
      "--------\n",
      "[[ 6.  6.]\n",
      " [ 1.  0.]\n",
      " [ 2.  0.]\n",
      " [ 0.  2.]\n",
      " [ 9.  7.]\n",
      " [ 2.  7.]]\n",
      "--------\n",
      "[[ 6.  1.  2.  0.  9.  2.]\n",
      " [ 6.  0.  0.  2.  7.  7.]]\n"
     ]
    }
   ],
   "source": [
    "a = np.floor(10*np.random.random((3, 4)))\n",
    "print(a)\n",
    "print('--------')\n",
    "print(a.ravel())# 转换为一维数组\n",
    "print('--------')\n",
    "a.shape = (6, 2)\n",
    "print(a)\n",
    "print('--------')\n",
    "print(a.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 9.  3.  9.  6.]\n",
      " [ 1.  2.  4.  9.]]\n",
      "[[ 9.  3.]\n",
      " [ 1.  2.]\n",
      " [ 9.  6.]\n",
      " [ 4.  9.]]\n"
     ]
    }
   ],
   "source": [
    "a = np.floor(10*np.random.random((2, 2)))\n",
    "b = np.floor(10*np.random.random((2, 2)))\n",
    "print(np.hstack((a, b)))# 横向叠加\n",
    "print(np.vstack((a, b)))# 纵向叠加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[ 6.,  2.,  7.,  3.],\n",
      "       [ 3.,  4.,  6.,  7.]]), array([[ 4.,  7.,  6.,  4.],\n",
      "       [ 0.,  4.,  9.,  9.]]), array([[ 3.,  1.,  0.,  6.],\n",
      "       [ 2.,  4.,  7.,  7.]])]\n",
      "[array([[ 6.,  2.,  7.],\n",
      "       [ 3.,  4.,  6.]]), array([[ 3.],\n",
      "       [ 7.]]), array([[ 4.,  7.,  6.,  4.,  3.,  1.,  0.,  6.],\n",
      "       [ 0.,  4.,  9.,  9.,  2.,  4.,  7.,  7.]])]\n",
      "[array([[ 7.,  1.],\n",
      "       [ 5.,  2.],\n",
      "       [ 3.,  0.],\n",
      "       [ 8.,  8.]]), array([[ 3.,  1.],\n",
      "       [ 2.,  8.],\n",
      "       [ 4.,  9.],\n",
      "       [ 1.,  7.]]), array([[ 0.,  2.],\n",
      "       [ 7.,  4.],\n",
      "       [ 4.,  8.],\n",
      "       [ 2.,  1.]])]\n"
     ]
    }
   ],
   "source": [
    "a = np.floor(10*np.random.random((2, 12)))\n",
    "print(np.hsplit(a, 3))# 横向切割\n",
    "print(np.hsplit(a, (3, 4)))\n",
    "a = np.floor(10*np.random.random((12, 2)))\n",
    "print(np.vsplit(a, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11]\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11]\n",
      "True\n",
      "(3, 4)\n",
      "100958920\n",
      "100958920\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(12)\n",
    "b = a\n",
    "print(a)\n",
    "print(b)\n",
    "print(a is b)\n",
    "b.shape = 3, 4\n",
    "print(a.shape)\n",
    "print(id(a))\n",
    "print(id(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "[[   0    1    2    3]\n",
      " [1234    5    6    7]\n",
      " [   8    9   10   11]]\n",
      "100958920\n",
      "100982600\n"
     ]
    }
   ],
   "source": [
    "c = a.view()# 复制操作\n",
    "print(c is a)\n",
    "c.shape = 2, 6\n",
    "c[0,4] = 1234\n",
    "print(a)\n",
    "print(id(a))# 值相同，地址不同\n",
    "print(id(c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[9999    1    2    3]\n",
      " [1234    5    6    7]\n",
      " [   8    9   10   11]]\n",
      "[[   0    1    2    3]\n",
      " [1234    5    6    7]\n",
      " [   8    9   10   11]]\n"
     ]
    }
   ],
   "source": [
    "d = a.copy()\n",
    "d is a\n",
    "d[0, 0] = 9999\n",
    "print(d)# 值不相同，地址也不相同\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.          0.84147098  0.90929743  0.14112001]\n",
      " [-0.7568025  -0.95892427 -0.2794155   0.6569866 ]\n",
      " [ 0.98935825  0.41211849 -0.54402111 -0.99999021]\n",
      " [-0.53657292  0.42016704  0.99060736  0.65028784]\n",
      " [-0.28790332 -0.96139749 -0.75098725  0.14987721]]\n",
      "[2 0 3 1]\n",
      "[ 0.98935825  0.84147098  0.99060736  0.6569866 ]\n"
     ]
    }
   ],
   "source": [
    "data = np.sin(np.arange(20)).reshape(5, 4)\n",
    "print(data)\n",
    "ind = data.argmax(axis=0)# 最大值的索引\n",
    "print(ind)\n",
    "data_max = data[ind, range(data.shape[1])]# 按照行，列查找值\n",
    "print(data_max)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0 10 20 30]\n",
      "[[ 0 10 20 30  0 10 20 30  0 10 20 30]\n",
      " [ 0 10 20 30  0 10 20 30  0 10 20 30]\n",
      " [ 0 10 20 30  0 10 20 30  0 10 20 30]\n",
      " [ 0 10 20 30  0 10 20 30  0 10 20 30]]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(0, 40, 10)\n",
    "print(a)\n",
    "b = np.tile(a, (4, 3))# 对ndarray进行扩展\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4 3 5]\n",
      " [1 2 1]]\n",
      "-------\n",
      "[[3 4 5]\n",
      " [1 1 2]]\n",
      "-------\n",
      "[[3 4 5]\n",
      " [1 1 2]]\n",
      "-------\n",
      "[2 3 1 0]\n",
      "-------\n",
      "[1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[4, 3, 5], [1, 2, 1]])\n",
    "print(a)\n",
    "print('-------')\n",
    "b = np.sort(a, axis=1)\n",
    "print(b)\n",
    "a.sort(axis=1)\n",
    "print('-------')\n",
    "print(a)\n",
    "a = np.array([4, 3, 1, 2])\n",
    "j = np.argsort(a)\n",
    "print('-------')\n",
    "print(j)\n",
    "print('-------')\n",
    "print(a[j])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
